package com.neuedu.model.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.multipart.MultipartFile;

import com.neuedu.model.mapper.ActivityMapper;
import com.neuedu.model.mapper.CandidateMapper;
import com.neuedu.model.mapper.ImagesMapper;
import com.neuedu.model.mapper.LwMapper;
import com.neuedu.model.mapper.LwrecordMapper;
import com.neuedu.model.mapper.PrizeMapper;
import com.neuedu.model.mapper.VoterecordMapper;
import com.neuedu.model.po.Activity;
import com.neuedu.model.po.Candidate;
import com.neuedu.model.po.Images;
import com.neuedu.model.po.Lw;
import com.neuedu.model.po.Lwrecord;
import com.neuedu.model.po.Prize;
import com.neuedu.model.po.Voterecord;

public class VoteServiceImpl implements VoteService {
	
	@Autowired
	private ActivityMapper activityMapper;
	
	@Autowired
	private CandidateMapper candidateMapper;
	
	@Autowired
	private ImagesMapper imagesMapper;
	
	@Autowired
	private VoterecordMapper voterecordMapper;
	
	@Autowired
	private PrizeMapper prizeMapper;
	
	@Autowired
	private LwMapper lwMapper;
	
	@Autowired
	private LwrecordMapper lwrecordMapper;

	@Override
	public Activity selectActivityById(int aid) throws Exception {
		//每次查询活动信息的时候，先更新活动点击量
		activityMapper.updateActivityAccess(aid);
		//查询活动信息
		Activity activity = activityMapper.selectActivityById(aid);
		return activity;
	}

	@Override
	public List<Candidate> selectCandidate(int aid, int pageNum, int pageSize) {
		return candidateMapper.selectCandidate(aid, (pageNum-1)*pageSize, pageSize);
	}

	@Override
	public List<Candidate> selectCandidateByName(String cname) {
		return candidateMapper.selectCandidateByName(cname);
	}

	@Override
	public void saveCandidate(Candidate candidate, MultipartFile[] upload, String uploadpath) {
		
		//1.文件上传
		for(int i=0;i<upload.length;i++){
			String filename = System.currentTimeMillis() + upload[i].getOriginalFilename();
			// 文件目标存放位置
			File f = new File(uploadpath,filename);
			
			try {
				//将内存中的数据写入磁盘
				//1.文件上传
				upload[i].transferTo(f);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(i==0){
				//获取第一张图片
				candidate.setImgurl(filename);
				//2.往选手表中，插入一条数据
				candidateMapper.addCandidate(candidate);
			}
			
			Images images = new Images();
			images.setCid(candidate.getCid());
			images.setImgurl(filename);
			//3.往图片表中，插入N条数据
			imagesMapper.addImage(images);
			
		}	
		//4.更新活动表，参赛选手人数+1
		activityMapper.updateActivityPeople(candidate.getAid());
		
	}

	@Override
	public Candidate selectCandidateById(int cid) {
		return candidateMapper.getCandidateById(cid);
	}

	@Override
	public void updateCandidate(int aid, int cid) {
		//更新选手的热度
		candidateMapper.updateCandidateHots(cid);
		//更新活动的访问量
		activityMapper.updateActivityAccess(aid);
	}
	
	public boolean canVote(int aid){
		try {
			Activity a = activityMapper.selectActivityById(aid);
			Date now = new Date();
			if(now.getTime()<a.getEndtime().getTime() && now.getTime()>a.getStarttime().getTime()){
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public void updateCandidateVoteInfo(int aid, int cid, String openid) {
		//1.更新活动表的totaltickets字段
		activityMapper.updateActivityTickets(aid);
		
		//2.更新选手表的tickets字段
		candidateMapper.updateCandidateTickets(cid);
		
		//3.在投票记录表中，增加一条数据
		Voterecord voterecord = new Voterecord();
		voterecord.setAid(aid);
		voterecord.setCid(cid);
		voterecord.setOpenid(openid);
		voterecord.setVotetime(new Date());
		
		voterecordMapper.saveVoterecord(voterecord);
		
	}

	@Override
	public List<Candidate> selectCandidateRank(int aid) {
		return candidateMapper.getCandidateRank(aid);
	}

	@Override
	public List<Prize> selectPrizeByAid(int aid) {
		return prizeMapper.findPrizeByAid(aid);
	}

	@Override
	public List<Lw> selectAllLw() {
		return lwMapper.getAllLw();
	}

	@Override
	public void updateCandidateGifts(int aid, int cid, int lwid, int lwcount, String openid) {
		
		//0.根据lwid得到礼物的详细信息
		Lw lw = lwMapper.getLwById(lwid);
		//1.更新选手表的礼物点数
		candidateMapper.updateCandidateGifts(cid, lw.getDianshu()*lwcount);
		//2.在礼物记录表中，加入一条数据
		Lwrecord lwrecord = new Lwrecord();
		lwrecord.setAid(aid);
		lwrecord.setCid(cid);
		lwrecord.setLwcount(lwcount);
		lwrecord.setLwid(lwid);
		lwrecord.setOpenid(openid);
		lwrecord.setPrice(lw.getPrice());
		lwrecord.setStime(new Date());
		
		lwrecordMapper.addLwrecord(lwrecord);
	}
	
	
	

}
